package both;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import both.map.TerrainType;
import both.map.mapView.Actor;
import both.map.mapView.MapBuilder;
import both.map.mapView.MapDisplay;
import both.map.mapView.MapEvent;
import both.map.mapView.MapListener;

public class MapTestWindow implements MapListener {

	private JFrame frame;
	private JPanel mapHolder;
	private MapDisplay map;
	private Actor actor;
	private MapBuilder builder;

	public MapTestWindow() {
		frame = new JFrame("Map Test");
		JPanel content = (JPanel) frame.getContentPane();
		BoxLayout layout1 = new BoxLayout(content, BoxLayout.LINE_AXIS);
		content.setLayout(layout1);

		builder = new MapBuilder();
		frame.add(builder);

		mapHolder = new JPanel();
		BoxLayout layout2 = new BoxLayout(mapHolder, BoxLayout.LINE_AXIS);
		mapHolder.setLayout(layout2);

		mapHolder.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5,
				Color.LIGHT_GRAY));

		map = new MapDisplay();

		mapHolder.setOpaque(true);
		mapHolder.add(map);
		frame.add(mapHolder);

		actor = new Actor();

		mapHolder.setPreferredSize(new Dimension(33 * 20 + 10, 33 * 20 + 10));
		mapHolder.setMaximumSize(new Dimension(33 * 20 + 10, 33 * 20 + 10));
		mapHolder.setSize(33 * 20 + 10, 33 * 20 + 10);
		mapHolder.setVisible(true);

		map.createFirstChunk();

		map.addMapListener(this);

		frame.setVisible(true);
		frame.pack();
		frame.setMinimumSize(frame.getSize());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		map.updateMapArea(0, 0, 0, 5, 6, TerrainType.stoneWall);
		map.updateMapArea(0, 1, 1, 4, 5, TerrainType.stoneFloor);
		map.updateMapArea(0, 6, 0, 12, 15, TerrainType.grass);
		map.updateMapArea(0, 13, 0, 18, 15, TerrainType.dirt);
		map.updateMapArea(0, 5, 3, 9, 3, TerrainType.cobble);
		map.updateMapArea(0, 9, 3, 9, 9, TerrainType.cobble);
		map.updateMapArea(0, 7, 9, 11, 13, TerrainType.cobble);
		map.updateMapArea(0, 8, 10, 10, 12, TerrainType.water);
		
		map.addActor(0, actor, 2, 2);
		map.moveActor(0, actor, 1, 1);
	}

	public static void main(String[] args) {
		new MapTestWindow();
	}

	@Override
	public void mapEventPerformed(MapEvent e) {
		if (!e.isArea()) {
			map.updateMap(0, e.getX(), e.getY(), builder.getSelected());
		} else {
			map.updateMapArea(0, e.getX(), e.getY(), e.getA(), e.getB(),
					builder.getSelected());
		}
	}
}
